var article = document.querySelector('.article')
var article_title = article.querySelector('.article_title')
var article_classify = article.querySelector('.article_classify')
var article_content = article.querySelector('.article_content')
var article_time = article.querySelector('.article_time')
var article_continue = article.getElementsByClassName('article_continue')[0];

var hot_list = document.getElementsByClassName('hot_list')[0];

var footer_list = document.getElementsByClassName('footer_list')[0]
var footer_pages = document.getElementsByClassName('footer_pages')[0]

var commend_list = document.getElementsByClassName('commend_list')[0]

var header_login = document.getElementsByClassName('header_login')[0]

header_login.addEventListener('click', function() {
    location.href = '/admin/'
})

function renderGetArticleData(result) {
    var arr = JSON.parse(result)
    var item = arr[0]
    article_title.innerHTML = item.title
    article_classify.innerHTML = item.classify
    article_content.innerHTML = item.content.slice(0, 100) + '...'
    article_time.innerHTML = item.time
    article_continue.setAttribute('data-id', item._id)
}

function renderGetHotArticleListData(result) {
    var arr = JSON.parse(result)
    arr.forEach((item, index) => {
        var li = document.createElement('li');
        li.innerHTML = item.title;
        li.className = 'hot_item';
        li.setAttribute('data-id', item._id)
        li.addEventListener('click', function() {
            var id = this.getAttribute('data-id')
            location.href = '/getPage?id=' + id;
        })
        hot_list.appendChild(li)
    })
}

function renderGetNewArticleListData(result) {
    footer_list.innerHTML = ""
    var arr = JSON.parse(result);
    arr.forEach((item, index) => {
        var li = document.createElement('li');
        li.innerHTML = item.title;
        li.className = "footer_item";
        li.setAttribute('data-id', item._id)
        li.addEventListener('click', function() {
            var id = this.getAttribute('data-id')
            location.href = '/getPage?id=' + id;
        })
        footer_list.appendChild(li)
    })
}

function renderArticleNumsData(result) {
    var num = Math.ceil(result / 5)
    for (var i = 1; i <= num; i++) {
        var li = document.createElement('li');
        li.innerHTML = i;
        li.className = 'pages';
        li.addEventListener('click', function() {
            var num = this.innerHTML;
            async function handle() {
                var getNewArticleListData = await myAjax('GET', '/getNewArticleList?num=' + num, null)
                renderGetNewArticleListData(getNewArticleListData)
            }
            handle()
        })
        footer_pages.appendChild(li)
    }
}

function renderGetNewCommendListData(result) {
    commend_list.innerHTML = ""
    var arr = JSON.parse(result);
    arr.forEach((item, index) => {
        var li = document.createElement('li');
        li.innerHTML = item.commend;
        li.className = "commend_item";
        li.setAttribute('data-id', item.fromId)
        li.addEventListener('click', function() {
            var id = this.getAttribute('data-id')
            location.href = '/getPage?id=' + id;
        })
        commend_list.appendChild(li)
    })
}

async function handle() {
    var getArticleData = await myAjax('GET', '/getArticle', null)
    renderGetArticleData(getArticleData)
    var getHotArticleListData = await myAjax('GET', '/getHotArticleList', null)
    renderGetHotArticleListData(getHotArticleListData)
    var getNewArticleListData = await myAjax('GET', '/getNewArticleList?num=1', null)
    renderGetNewArticleListData(getNewArticleListData)
    var ArticleNumsData = await myAjax('GET', '/ArticleNums', null)
    renderArticleNumsData(ArticleNumsData)
    var getNewCommendListData = await myAjax('GET', '/getNewCommendList', null)
    renderGetNewCommendListData(getNewCommendListData)
}
handle()

article_continue.addEventListener('click', function() {
    var id = this.getAttribute('data-id')
    location.href = '/getPage?id=' + id;
})